﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.ICM.ViewModel;
using KPIS.GERP.Models.SYS;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.ServiceModel;
using System.Web.Mvc;

namespace KPIS.GERP.WCF.GIMS
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MasDepreciationTypeService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select MasDepreciationTypeService.svc or MasDepreciationTypeService.svc.cs at the Solution Explorer and start debugging.
    [ServiceContract]
    public interface IMasDepreciationTypeService
    {
        [OperationContract]
        IEnumerable<KPIS.GERP.Models.ICM.MasDepreciationType> SelectMasDepreciationType(MasDepreciationType criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int Insert(MasDepreciationType model, int intCreateByUserId);

        [OperationContract]
        int Update(MasDepreciationType model, int updateByUserId);

        [OperationContract]
        void Delete(int id, int deleteByUserId);
    }


    [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class MasDepreciationTypeService : IMasDepreciationTypeService
    {

        public IEnumerable<KPIS.GERP.Models.ICM.MasDepreciationType> SelectMasDepreciationType(MasDepreciationType criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<MasDepreciationType> query = context.MasDepreciationTypes;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public int Insert(MasDepreciationType model, int intCreateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var existData = context.MasDepreciationTypes
                        .Where(m => m.Name == model.Name
                            && m.RecordStatus != RecordStatus.DeletedStatus).ToList();

                    if (existData.Count == 0)
                    {
                        model.CreatedBy = intCreateByUserId;
                        model.RecordStatus = RecordStatus.UsedStatus;
                        model.CreatedWhen = DateTime.Now;
                        context.MasDepreciationTypes.Add(model);
                        context.SaveChanges();
                        return model.DpTypeId;
                    }
                    else
                    {
                        throw new Exception("เกิดข้อผิดพลาด!! ข้อมูลนี้มีบันทึกอยู่แล้ว");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public int Update(MasDepreciationType model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var Name = context.MasDepreciationTypes
                               .Where(m => m.DpTypeId == model.DpTypeId
                                   && m.RecordStatus != RecordStatus.DeletedStatus)
                                .Select(m => m.Name).FirstOrDefault();

                    if (model.Name == Name)
                    {
                        model.UpdatedBy = updateByUserId;
                        model.UpdatedWhen = DateTime.Now;
                        context.Entry(model).State = EntityState.Modified;
                        context.SaveChanges();
                        return model.DpTypeId;
                    }
                    else
                    {
                        var existData = context.MasDepreciationTypes
                            .Where(m => m.Name == model.Name
                                && m.RecordStatus != RecordStatus.DeletedStatus).ToList();

                        if (existData.Count == 0)
                        {
                            model.UpdatedBy = updateByUserId;
                            model.UpdatedWhen = DateTime.Now;
                            context.Entry(model).State = EntityState.Modified;
                            context.SaveChanges();
                            return model.DpTypeId;
                        }
                        else
                        {
                            throw new Exception("เกิดข้อผิดพลาด!! ข้อมูลนี้มีบันทึกอยู่แล้ว");
                        }
                    }


                }
            }
            catch (Exception ex)
            {
                throw ex; 
            }

        }

        public void Delete(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.MasDepreciationTypes.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    modelToDelete.RecordStatus = Models.SYS.RecordStatus.DeletedStatus;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

    
    }
}
